package com.project.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.project.dao.ReaderInfoDAO;
import com.project.entity.DeptInfo;
import com.project.entity.ReaderInfo;
import com.project.entity.Role;

/**
 * @author jackiel
 * 
 */
@Repository("readerInfoDAO")
public class ReaderInfoDAOImpl extends GenericDAOImpl<ReaderInfo, Integer>
		implements ReaderInfoDAO {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.project.dao.ReaderInfoDAO#getUsrInfoByNameAndPsw(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public ReaderInfo getReaderInfoByNameAndPsw(String name, String password) {
		String hql = "from ReaderInfo where name=? and password=?";
		ReaderInfo readerInfo = executeUniqueQuery(hql, name, password);

		return readerInfo;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.project.dao.ReaderInfoDAO#getReaderList()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ReaderInfo> getReaderList() {

		String hql = "select r.id, r.name, r.sex, r.phone, r.email, r.deptId, r.password, dr.name, r.deleteDateTime "
				+ "from ReaderInfo r left join r.deleteReader dr";

		List<Object[]> list = getSession().createQuery(hql).list();
		// .setMaxResults(10)
		// .setFirstResult(firstResult)

		List<ReaderInfo> readerlist = new ArrayList<ReaderInfo>();
		for (Object[] objects : list) {
			ReaderInfo readerInfo = new ReaderInfo();
			readerInfo.setId((Integer) objects[0]);
			readerInfo.setName(objects[1].toString());
			readerInfo.setSex(objects[2].toString());
			readerInfo.setPhone(objects[3].toString());
			readerInfo.setEmail(objects[4].toString());
			readerInfo.setDeptId((DeptInfo) objects[5]);
			readerInfo.setPassword(objects[6].toString());
			if (objects[7] != null) {
				ReaderInfo deleteInfo = new ReaderInfo();
				deleteInfo.setName((String) objects[7]);
				readerInfo.setDeleteReader(deleteInfo);
			} else {
				readerInfo.setDeleteReader(null);
			}
			readerInfo.setDeleteDateTime((Date) objects[8]);
			readerlist.add(readerInfo);
		}

		return readerlist;
	}

	@Override
	public ReaderInfo get(String name) {
		String hql = "from ReaderInfo where name=?";
		ReaderInfo readerInfo = (ReaderInfo) executeUniqueQuery(hql, name);
		return readerInfo;
	}

	@Override
	public ReaderInfo getRole(String name) {
		String hql = "select re.id,re.name,r.name,r.brwAmtLimit from ReaderInfo re,Role r"
				+ "  where re.roleId.id=r.id and re.name=?";
		Object[] obj = (Object[]) getSession().createQuery(hql)
				.setString(0, name).uniqueResult();
		ReaderInfo reader = new ReaderInfo();
		Role role = new Role();
		if (obj == null) {
			return null;
		} else {
			if (obj[0] != null) {
				reader.setId((Integer) obj[0]);
			}
			if (obj[1] != null) {
				reader.setName(obj[1].toString());
			}
			if (obj[2] != null) {
				role.setName(obj[2].toString());
			}
			if (obj[3] != null) {
				role.setBrwAmtLimit(Integer.parseInt(obj[3].toString()));
			}
			reader.setRoleId(role);
			return reader;
		}

	}
}
